﻿using AntMovement.Core.Entity;
using AntMovement.Core.Logic.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.SDK.LTSMC.Logic
{
    public class Coil : ICoil
    {
        private DO _do;
        public Coil(DO dO)
        {
            _do = dO;
        }

        public bool Status => _do.Status;

        public string Execute(bool bl)
        {
            if (_do == null)
            {
                throw new Exception("对象为空");
            }
            if (bl == _do.Status)
            {
                return "ok";
            }
            if (!_do.Forbidden)
            {
                short error = LTSMC.smc_write_outbit((ushort)_do.CardNo, (ushort)((_do.MdlIndex * 16) + _do.OrderlyNum), (ushort)(bl ? 0 : 1));
                if (error == 0)
                {
                    return "ok";
                }
                throw new Exception($"CardNo[{_do.CardNo}] Index[{((_do.MdlIndex * 16) + _do.OrderlyNum)}] SLDM_Set_OutBit ==>> {error}");
            }
            return "ok";
        }
    }
}
